<template>
  <!-- 表格头部 -->
  <div class="hy-table">
    <slot name="header">
      <div class="title">{{ title }}</div>
      <div class="handler">
        <slot name="headerHandler"></slot>
      </div>
    </slot>
  </div>
  <!-- 表格 -->
  <div class="body">
    <el-table
      :data="listData"
      border
      @selection-change="handleSelectionChange"
      v-bind="childrenProps"
    >
      <!-- 表格列是否显示可勾选 -->
      <el-table-column
        v-if="showSelectColumn"
        type="selection"
        align="center"
        width="60"
      >
      </el-table-column>
      <el-table-column
        v-if="showIndexColumn"
        type="index"
        label="序号"
        align="center"
        width="80"
      >
      </el-table-column>
      <template v-for="propItem in propList" :key="propItem.prop">
        <el-table-column v-bind="propItem" align="center" show-overflow-tooltip>
          <template #default="scope">
            <slot :name="propItem.slotName" :row="scope.row">
              {{ scope.row[propItem.prop] }}
            </slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
  </div>

  <!-- 表格尾部 -->
  <div class="footer" v-if="showFooter">
    <slot name="footer">
      <!-- 表格尾部的分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-size="page.pageSize"
        :page-sizes="[10, 20, 30]"
        layout="total,sizes,prev,pager,next,jumper"
        :total="listCount"
      ></el-pagination>
    </slot>
  </div>
</template>
<script lang="ts" setup>
interface Props {
  title?: string;
  listData: Array<any>;
  propList: Array<any>;
  listCount?: number;
  page?: { currentPage: number; pageSize: number };
  childrenProps?: any;
  showIndexColumn?: boolean;
  showSelectColumn?: boolean;
  showFooter?: boolean;
}
const props = withDefaults(defineProps<Props>(), {
  title: "",
  listData: () => [],
  listCount: 0,
  page: () => ({ currentPage: 0, pageSize: 10 }),
  childrenProps: () => ({}),
  showIndexColumn: true,
  showSelectColumn: true,
  showFooter: true,
  propList: () => [],
});
const emit = defineEmits<{
  (e: "selectionChange", value: any): void;
  (e: "update:page", value: any): void;
}>();
const handleSelectionChange = (value: any) => {
  emit("selectionChange", value);
};
const handleCurrentChange = (currentPage: number) => {
  emit("update:page", { ...props.page, currentPage });
};
const handleSizeChange = (pageSize: number) => {
  emit("update:page", { ...props.page, pageSize });
};
defineExpose({
  handleSelectionChange,
  handleCurrentChange,
  handleSizeChange,
});
</script>
<style scoped>
.hy-table {
  padding: 0 20px;
}
.handler {
  margin-top: 16px;
  margin-bottom: 16px;
}
.body {
  padding: 0 20px;
  margin-bottom: 16px;
}
.footer {
  padding: 0 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
